package cn.ilikes.tools.nosql.jpa.clients.indexs.units;

import org.apache.lucene.document.Document;

import org.apache.lucene.document.Field;
import org.slf4j.Logger;

import cn.ilikes.tools.logs.Loggerfactory;
import cn.ilikes.tools.nosql.jpa.clients.indexs.IClientIndex;
import cn.ilikes.tools.nosql.jpa.graphs.Node;
import cn.ilikes.tools.nosql.jpa.metadatas.EntityMetaData;
import cn.ilikes.tools.nosql.jpa.unitls.PropertyAccessorHelper;

public class LuceneEntityClassToDocument {

	private static Logger logger = Loggerfactory.instance(LuceneEntityClassToDocument.class);

	/**
	 * 
	 * @param document
	 * @param metadata
	 * @param entity
	 * @param parentId
	 * @param clazz
	 * @return
	 */

	public Document addEntityClassToDocument(Document document, Node node) throws IllegalArgumentException {
		try {
			Object entity = Node.getEntity(node);
			EntityMetaData metadata = Node.getEntityMetaData(node);
			String id = LuceneQueryUtils.toLowerCase(PropertyAccessorHelper.getId(entity, metadata));
			//			Field luceneField = new Field(IClientIndex.ENTITY_ID_FIELD,LuceneQueryUtils.toLowerCase(id), Field.Store.YES, Field.Index.NOT_ANALYZED, Field.TermVector.NO);
			Field luceneField = new Field(IClientIndex.ENTITY_ID_FIELD, id, Field.Store.YES, Field.Index.NOT_ANALYZED, Field.TermVector.NO);
			if (Loggerfactory.isdebug(logger))
				Loggerfactory.debug(logger, "add  EntityClass  To  Document  lucene field : " + IClientIndex.ENTITY_ID_FIELD + " value : " + id.toString());
			document.add(luceneField);

			String kunderaId = LuceneQueryUtils.toLowerCase(LuceneQueryUtils.getJPAHBaseKeyofId(metadata, id));
			luceneField = new Field(IClientIndex.JPA_SQL_ID_FIELD, kunderaId, Field.Store.YES, Field.Index.NOT_ANALYZED, Field.TermVector.NO);
			if (Loggerfactory.isdebug(logger))
				Loggerfactory.debug(logger, "add EntityClass  To  Document  lucene field : " + IClientIndex.JPA_SQL_ID_FIELD + " value : " + kunderaId);
			document.add(luceneField);

			String entityClazz = LuceneQueryUtils.toLowerCase(metadata.getEntityClazz().getCanonicalName());
			luceneField = new Field(IClientIndex.ENTITY_CLASS_FIELD, entityClazz, Field.Store.YES, Field.Index.NOT_ANALYZED, Field.TermVector.NO);
			if (Loggerfactory.isdebug(logger))
				Loggerfactory.debug(logger, "add EntityClass  To  Document  lucene field : " + IClientIndex.ENTITY_CLASS_FIELD + " value : " + entityClazz);
			document.add(luceneField);

			String time = LuceneQueryUtils.toLowerCase(Long.toString(System.currentTimeMillis()));
			luceneField = new Field(IClientIndex.ENTITY_INDEX_TIMESAMP, time, Field.Store.YES, Field.Index.NOT_ANALYZED, Field.TermVector.NO);
			document.add(luceneField);
			if (Loggerfactory.isdebug(logger))
				Loggerfactory.debug(logger, "add EntityClass  To  Document  lucene field : " + IClientIndex.ENTITY_INDEX_TIMESAMP + " value : " + time);

			String indexName = LuceneQueryUtils.toLowerCase(metadata.getEntityIndex().getIndexName());
			luceneField = new Field(IClientIndex.ENTITY_INDEXNAME_FIELD, indexName, Field.Store.NO, Field.Index.NOT_ANALYZED);
			if (Loggerfactory.isdebug(logger))
				Loggerfactory.debug(logger, "add EntityClass  To  Document  lucene field : " + IClientIndex.ENTITY_INDEXNAME_FIELD + " value : " + indexName);
			document.add(luceneField);
			return document;
		} catch (Exception e) {
			Loggerfactory.error(logger, e);
			throw new IllegalArgumentException("add entity class   to   document  error : " + e.getMessage(), e);
		}
	}

}